/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef CONNECTION_H
#define CONNECTION_H

#include <QObject>
#include <QtNetwork/QTcpSocket>
#include "line.h"
#include "tagcache.h"

class Core;

/**
 * Zapuzdruje pripojenie k talkru.
 * @author Ziman & Xloverin <ziman@centrum.sk, xloverin@gmail.com>
 */
class Connection: public QObject {

	/// Socket na pripojenie k serveru.
	QTcpSocket *socket;

	/// Sluzi na ukladanie zatial nespracovatelnych dat pri riadkovom citani.
	QByteArray buffer;
	
	/// Pointer na jadro.
	Core *m_core;

	Q_OBJECT
public:

    Connection(Core *core);
    virtual ~Connection();

	/// Otvori pripojenie k zadanemu serveru.
	void open(QString host, int port);

	/// Oznami, ci je pripojeny k serveru.
	bool active();

	/// Uzavrie pripojenie.
	void close();

signals:
	void connected();
	void disconnected();
	
	/**
	 * Emituje sa, ked prisli data zo socketu.
	 * @param data Precitane data.
	 */
	void lineDataReady(const QByteArray &data);
	void charDataReady(const QByteArray &data);

public slots:
	virtual void write(QString data);

private slots:
	virtual void on_socket_readyRead();
};

#endif
